i'm a newbie to c++.i want to know that is there any differnce between endl,"\n" and '\n'.(although all are used to place the cursor to newline).any help will be appreciated.
i'm a newbie to c++.i want to know that is there any differnce between endl,"\n" and '\n'.(although all are used to place the cursor to newline).any help will be appreciated.
'\n' is a single newline character.
"\n" is a string containing only a single newline character.
std::endl is an iostream manipulator that, when inserted into a stream, writes a newline and then flushes the stream.
Use endl if you want a newline and want to force immediate display of the output (or you want to safeguard against data loss in the face of a crash, i.e. when writing debug information).
All other times, in particular when your next operation is writing yet another thing, use the newline character directly.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
Forcing a stream to flush is rarely necessary in C++, it is almost always done automatically in the cases that you need it. Because of this I almost always use '\n' and if I really want a flush I just send std::flush to the stream instead of std::endl to make it clear that I'm trying to force a flush specifically.